import moment from 'moment';
import { Popover, Tag } from 'antd';
import { messages } from 'share/common';
import constants from 'share/constants';
import paymentService from '../payment-service'
import React from 'react';

const payStatus = {
  N: messages('expense-18.key1')/*未付款*/,
  P: messages('expense-18.key2')/*付款中*/,
  S: messages('expense-18.key3')/*已付款*/
};

export const sourcesMap = new Map([
  ['payment-request', {
    searchForm: ['formOID', 'submittedDate', 'applicantOIDs', 'title', 'status'],
    columns: ['submittedDate', 'applicantName', 'formName', 'title', 'businessCode', 'currencyCode', 'totalAmount', 'status'],
    service: paymentService.getPaymentList
  }],
  ['payment-pay-info',{
    columns:['sequence','paymentCurrencyCode','amount','payeeCode','accountNumber','payArr','planedPaymentDate','remark','expBusinessCode', 'statusPay','operation'],
    service: paymentService.getPaymentPayInfo,
    searchForm:[]
  }]
]);

export const columnsMap = new Map([
  ['submittedDate',
    {
      title: messages('common.submit.date')/*提交日期*/,
      dataIndex: 'submittedDate',
      render: (value, record) => moment(record.lastSubmittedDate || record.createdDate).format('YYYY-MM-DD'),
      width: 120
    }
  ],
  ['formName',
    {
      title: messages('common.document.name')/*单据名称*/,
      dataIndex: 'formName',
      render: value => value ? <Popover placement="topLeft" content={value}>{value}</Popover> : '-'
    }
  ],
  ['businessCode',
    {
      title: messages('common.document.code')/*单号*/,
      dataIndex: 'businessCode',
      render: value => value ? <Popover placement="topLeft" content={value}>{value}</Popover> : '-'
    }
  ],
  ['currencyCode',
    {
      title: messages('common.currency')/*币种*/,
      dataIndex: 'currencyCode',
      width: 60
    }
  ],
  ['totalAmount',
    {
      title: messages('common.amount')/*金额*/,
      dataIndex: 'totalAmount',
      align: 'right',
      width: '7%'
    }
  ],
  ['status',
    {
      title: messages('common.column.status')/*状态*/,
      dataIndex: 'status',
      render: (value, record) => constants.documentStatus.map(item => {
        if (
          (item.value === String(value) && record.rejectType === 1000) ||
          item.value === String(value * 10000 + record.rejectType)
        ) {
          return <Tag color={item.color} style={{ margin: 0 }}>{item.text}</Tag>;
        }
      })
    }
  ],
  ['sequence',
    {
      title: messages('common.sequence')/*序号*/,
      dataIndex: 'sequence',
      width: 80,
      render: (value, record, index) => index + 1
    }
  ],
  ['amount',
    {
      title: messages('expense-18.key4')/*付款金额*/,
      dataIndex: 'amount',
      width: 80,
      render: (value) => value ? <Popover content={value}>{value}</Popover> : '-'
    }
  ],
  ['paymentCurrencyCode',
    {
      title: messages('common.currency')/*币种*/,
      dataIndex: 'paymentCurrencyCode',
      width: 60,
      render: (value) => value ? <Popover content={value}>{value}</Popover> : '-'
    }
  ],
  ['payeeCode',
    {
      title: messages('expense-18.key5')/*收款对象*/,
      dataIndex: 'payeeCode',
      render: (index, record) => <ColumnPayObject record ={record}/>
    }
  ],
  ['accountNumber',
    {
      title: messages('expense-18.key6')/*收款账号*/,
      dataIndex: 'accountNumber',
      render: (index, record) => <ColumnAccountNumber record ={record}/>
    }
  ],
  ['payArr',
    {
      title: messages('expense-18.key7')/*付款属性*/,
      dataIndex: 'payArr',
      render: (index, record) => <ColumnPayAttributes record={record}/>
    }
  ],
  ['remark',
    {
      title: messages('common.remark')/*备注temp*/,
      dataIndex: 'remark',
      render: value => value ? <Popover placement="topLeft" content={value}>{value}</Popover> : '-'
    }
  ],
  ['expBusinessCode',
    {
      title: messages('expense-18.key8')/*关联报销单*/,
      dataIndex: 'expBusinessCode',
      render: value => value ? <Popover placement="topLeft" content={value}>{value}</Popover> : '-'
    }
  ],
  ['statusPay',
    {
      title: messages('expense-18.key9')/*支付状态*/,
      dataIndex: 'statusPay',
      render: (index, record)  => record.status ? <Popover placement="topLeft" content={payStatus[record.status]}>{payStatus[record.status]}</Popover> : '-'
    }
  ],
  ['operation',
    {
      title: messages('common.operation')/*操作*/,
      dataIndex: 'operation',
    }
  ],
  ['planedPaymentDate',
    {
      title: messages('expense-18.key10')/*计划付款日期*/,
      dataIndex: 'planedPaymentDate',
      render: (value) => value ? moment(value).format('YYYY-MM-DD') : '-'
    }
  ],
  ['applicantName',
    {
      title: messages('common.applicant')/*申请人*/,
      dataIndex: 'applicantName',
      render: value => value ? <Popover placement="topLeft" content={value}>{value}</Popover> : '-'
    }
  ],
  ['title',
    {
      title: messages('expense-18.key11')/*事由*/,
      dataIndex: 'title',
      render: value => value ? <Popover placement="topLeft" content={value}>{value}</Popover> : '-'
    }
  ]

]);

export const searchMap = new Map([
  ['formOID',
    {
      id: 'formOID',
      label: messages('common.document.name')/*单据名称*/, component: 'Selector',
      props: {
        type: 'proxyForm',
        params: { formType: 105, isAll: true }
      },
      dataIndex: 'formName'
    }],
  ['submittedDate',
    {
      id: 'submittedDate', label: messages('common.submit.date')/*提交日期*/,
      component: 'DateRangePicker',
      initialValue: [moment().subtract(1, 'month'), moment()],
      parser: value => ( value && value[0] ? {
        startDate: value && value[0] ? moment(value[0]).format('YYYY-MM-DD 00:00:00') : null,
        endDate: value && value[1] ? moment(value[1]).format('YYYY-MM-DD 23:59:59') : null
      } : void 0),
      dataIndex: 'submittedDate'
    }],
  ['applicantOIDs',
    {
      id: 'applicantOIDs', label: messages('common.applicant')/*申请人*/,
      component: 'Chooser',
      props: {
        type: 'user_condition',
        labelKey: 'fullName',
        valueKey: 'userOID',
        listExtraParams: {
          roleType: 'TENANT'
        },
        dataIndex: 'applicantName'
      },
      parser: value => ({
        applicantOIDs: value ? value.map(item => item.userOID) : null
      }),
      JSONObjectFormatterParams: ['fullName', 'userOID']
    }
  ],
  ['title',
    {
      id: 'title', label: messages('expense-18.key11')/*事由*/,
      component: 'Input',
    }
  ],
  ['status',
    {
      id: 'status',
      label: messages('common.column.status')/*状态*/,
      initialValue: 'all',
      row: true,
      dataIndex: 'status'
    }
  ]
]);


//付款对象
function ColumnPayObject(record) {
  const messagesObj = {
    '1002': messages('expense-18.key12')/*员工*/,
    '1001': messages('expense-18.key13')/*供应商*/
  };
  return (
    <div style={{ textAlign: 'center' }}>
      <Tag color="black">
        {messagesObj.hasOwnProperty(record.record.payeeType) ? messagesObj[record.record.payeeType] : messages('expense-18.key14')/*未知*/ }
      </Tag>
      <br/>
      <Popover content={record.record.payeeName}>{record.record.payeeName}</Popover>
    </div>
  );
}


//收款账号
function ColumnAccountNumber (record) {
  const element = (
    <div>
      <span>{messages('expense-18.key15')/*户名*/}: {record.record.payeeBankAccountName}</span><br/>
      <span>{messages('expense-18.key16')/*账号*/}: {record.record.payeeBankAccountNumber}</span>
    </div>
  );
  return (
    <Popover content={element}>{element}</Popover>
  )
}

// 付款类型
function ColumnPayAttributes(record) {
  const paymentMethodCategory =
    {
      ONLINE_PAYMENT: messages('expense-18.key17')/*线上*/,
      OFFLINE_PAYMENT: messages('expense-18.key18')/*线下*/,
      EBANK_PAYMENT: messages('expense-18.key19')/*落地文件*/
    };
  let element = (
    <div>
      <span>{messages('expense-18.key20')/*付款方式类型*/}: {paymentMethodCategory[record.record.paymentMethod]}</span><br/>
      <span>{messages('expense-18.key21')/*付款类型*/}: {record.record.cashTransactionClassDTO && record.record.cashTransactionClassDTO.description}</span>
    </div>
  );
  return (
    <Popover content={element}>{element}</Popover>
  );
}
